/*
 * @author ccpalu
 */

function createChList(){
	if (document.getElementById('chList')==null){
		remove_element_by_id("chList");
		var chList = document.createElement("div");
		chList.id = "chList";
		chList.visible=false;
		chList.style.display="none";
		chList.style.zIndex=100;
		chList.innerHTML = '<img id="chBgImg" src="http://cnviewer.googlecode.com/hg/Images/chBg.jpg"\>';//src="http://cnviewer.googlecode.com/hg/Images/chBg.jpg"\>';
		document.body.appendChild(chList);
		var img = document.getElementById("chBgImg");
		img.onload = function(){
			img.height = 50 * 24;
			img.width = 620;
			setPosition(img, 214, 155);
		}
		for (var i = 1; i <= 22; i = i + 1) {
			chRoom(i);
		}
		buttonImage('UCSC', 'http://cnviewer.googlecode.com/hg/Images/ucsc.png', 'accessUCSC()', 45, 65, 560, 180,0,chList).title='Explore the regions of the selected chromosomes in the UCSC Genome Browser';
		buttonImage('addMark', 'http://cnviewer.googlecode.com/hg/Images/addMarkTur.png', 'addMarkCh()', 45, 65, 620, 180,0,chList).title='Add more selection marks to the checked chromosomes';
		buttonImage('rmvMark', 'http://cnviewer.googlecode.com/hg/Images/rmvMarkRed.png', 'rmvMarkCh()', 45, 65, 680, 180,0, chList).title='Remove a pair of selection marks from the checked chromosomes';
		buttonImage('rmvMark', 'http://cnviewer.googlecode.com/hg/Images/rstMarkGreen.png', 'rstMarkCh()', 45, 65, 740, 180,0, chList).title="Reset the selection marks' position of the checked chromosomes";
		addHelp ('hCh', 'help', 'http://sites.google.com/site/cnviewerguide/analysis/chromosomes', 'chList', 190, 805, 15);
		
		document.getElementById('chListButtonImg').src='http://cnviewer.googlecode.com/hg/Images/chPositionHid.png';

		var chPosButton=document.getElementById('chListButton');
		chPosButton.disable=false;
		chPosButton.style.zIndex=0;
	}
	else{
		for (var ch = 1; ch <= 22; ch = ch + 1) {
			if (ch<10)
				var id="0"+ch;
			else
				var id= ch;
			cnv = document.getElementById("CNV");
			chromossome = document.getElementById("ch"+id+"obj");
			chromossome.limitMin = cnv.min[ch] * cnv.realSize.rate;
			chromossome.limitMax = cnv.max[ch] * cnv.realSize.rate;
		}

	}

	return false;
}

function addMarkCh(){
	var chChecked = markChecked();
	if (chChecked ) {
		var cnv= document.getElementById('CNV');
		var surface, chromossomes, markW=4, img, ch, rate;

		for (var i = 0; i < chChecked.length; i = i + 1) {
			ch = chChecked[i];
			surface = dojo.byId('chSurf' + ch);
			if (ch < 10) 
				var id = '0'+ch;
			else
				var id= ch; 

			chromossomes = document.getElementById("ch" + id + "obj");
			img = document.getElementById("ch" + id + "Img");
			chromossomes.positions[chromossomes.positions.length] = Math.round(chromossomes.limitMin)
			chromossomes.positions[chromossomes.positions.length] = Math.round(chromossomes.limitMax);

			remove_element_by_id('chSurf'+ch);
			var surfNode = document.getElementById('chSurf'+ch+'node');

			var surface = dojox.gfx.createSurface(surfNode,img.width+(markW*2)+ "px", img.height+4+ "px");
			surface.getEventSource().setAttribute('id','chSurf'+ch);

			rate = cnv.realSize[ch] / img.width;

			for (var j = 0; j < chromossomes.positions.length; j = j + 1) {
				var shape = surface.createPath("m" + chromossomes.positions[j] + " 0 " + markW * 2 + " 0 " + markW * (-2) + " " + (img.height + 4) + " " + markW * 2 + " 0 " + markW * (-2) + " " + ((img.height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
					color: "black",
					width: 1.5 + "px",
					cap: "butt",
					join: 4
				}).setTransform({
					dx: 0,
					dy: 0
				});
				if (j<9)
					shape.getEventSource().setAttribute('id', 'ch' + id + 'mark0' + (j+1));
				else
					shape.getEventSource().setAttribute('id', 'ch' + id + 'mark' + (j+1));
				new dojox.gfx.MoveableLimited(shape);
				cnv.chRegions[ch][j] = Math.round(chromossomes.positions[j]*rate);
			}
			var tempArr = copyArray(cnv.chRegions[ch])
			cnv.chRegions[ch] = quickSort(tempArr);
		}
	}
	return false;
}

function rmvMarkCh(){
	var chChecked = markChecked();
	if (chChecked ) {
		var cnv= document.getElementById('CNV');
		var surface, chromossomes, index, ch;

		for (var i = 0; i < chChecked.length; i = i + 1) {
			ch = chChecked[i];
			surface = dojo.byId('chSurf' + ch);
			if (ch < 10) 
				var id = '0'+ch;
			else
				var id= ch; 

			chromossomes = document.getElementById("ch" + id + "obj");
			if (chromossomes.positions.length){
				chromossomes.positions.length = chromossomes.positions.length-2;
				index = surface.childNodes.length-2;
				remove_element_by_id('ch' + id + 'mark0' + (index));
				remove_element_by_id('ch' + id + 'mark0' + (index+1));
				remove_element_by_id('ch' + id + 'mark' + (index));
				remove_element_by_id('ch' + id + 'mark' + (index+1));

				chromossomes.regions();		
			}
		}
	}
	return false;
}

function rstMarkCh(){

	var chChecked = markChecked();
	if (chChecked ) {
		var cnv= document.getElementById('CNV');
		var surface, chromossomes, img, markW=4, ch;

		for (var i = 0; i < chChecked.length; i = i + 1) {
			ch = chChecked[i];
			surface = dojo.byId('chSurf' + ch);
			if (ch < 10) 
				var id = '0' + ch;
			else 
				var id = ch;

			chromossomes = document.getElementById("ch" + id + "obj");
			img = document.getElementById("ch" + id + "Img");
			chromossomes.positions = [];
			chromossomes.positions[0] = Math.round(chromossomes.limitMin)
			chromossomes.positions[1] = Math.round(chromossomes.limitMax);
			cnv.chRegions[ch]=[];

			remove_element_by_id('chSurf' + ch);
			var surfNode = document.getElementById('chSurf' + ch + 'node');

			var surface = dojox.gfx.createSurface(surfNode, img.width + (markW * 2) + "px", img.height + 4 + "px");
			surface.getEventSource().setAttribute('id', 'chSurf' + ch);

			rate = cnv.realSize[ch] / img.width;

			for (var j = 0; j < 2; j = j + 1) {
				var shape = surface.createPath("m" + chromossomes.positions[j] + " 0 " + markW * 2 + " 0 " + markW * (-2) + " " + (img.height + 4) + " " + markW * 2 + " 0 " + markW * (-2) + " " + ((img.height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
					color: "black",
					width: 1.5 + "px",
					cap: "butt",
					join: 4
				}).setTransform({
					dx: 0,
					dy: 0
				}); 
				shape.getEventSource().setAttribute('id', 'ch' + id + 'mark0' + (j + 1));
				new dojox.gfx.MoveableLimited(shape);
				cnv.chRegions[ch][j] = Math.round(chromossomes.positions[j] * rate);
			}
		}
	}		
	return false;
}

function markChecked(){
	var chChecked = new Array();

	for (var i=1, j=0; i<10; i = i+1){
		if (document.getElementById('chListCheck' + i).checked) {
			chChecked[j] = i;
			j=j+1;
		}
	}

	for (i=10; i<=22; i = i+1){
		if (document.getElementById('chListCheck' + i).checked) {
			chChecked[j] = i;
			j=j+1;
		}
	}
	if (chChecked.length == 0){
		alert('It is necessary to check at least one chromosomes before pressing this button.\n' + 
		'Using the blue marks you can also specify a genomic region.');
	}

	return chChecked;
}

function chListVisibility(){

	if (!document.getElementById('chListButton').disable) {

		var chs = document.getElementById('chList').childNodes;
		var disp;
		if (document.getElementById('chList').visible){
			document.getElementById('chList').visible = false;

			document.getElementById('chListButtonImg').src = 'http://cnviewer.googlecode.com/hg/Images/chPositionHid.png';
			document.getElementById('chListButtonImg').style.left = "197px"

				disp = "none";


		}
		else {
			document.getElementById('chList').visible = true;
			document.getElementById('chListButtonImg').src = 'http://cnviewer.googlecode.com/hg/Images/chPositionSh.png';
			document.getElementById('chListButtonImg').style.left = "834px";
			disp = "";	
		}

		for (var i = 0; i < chs.length; i = i + 1) {
			chs[i].style.display = disp;
		}
		document.getElementById('chList').style.display = disp;
		//document.getElementById('chListButton').style.display
	}

	return false;
}

function chRoom(ch){
/* creating each chromosome and its elements*/
	
	var cnv = document.getElementById("CNV");
	var chromossomes=document.createElement('OBJECT');
	var posit=255;
	var markW=4;
	if ( window.navigator.appName!='Opera')
		var chControlTop = 198;
	else
		var chControlTop = 210;
	chromossomes.posit=posit;
	chromossomes.ch=ch;
	chromossomes.style.display='none';

	if (ch<10)
		var id="0"+ch;
	else
		var id= ch;
	chromossomes.id="ch"+id+"obj";
	chromossomes.positions=new Array(); // array that stores location of the markers in the chromosome
	document.body.appendChild(chromossomes);

	/* identify the chromosome by its number*/
	var chNumberP = createParagraph('ch'+id+'number', ch , 'chList');
	setPosition(chNumberP, posit-20,(chControlTop + 15+ 50 * ch));
	setFont(chNumberP,'16px',0,0,'bolder');

	/* it creats a check box for chromosome selection*/
	setPosition(createParagraph('chListCheck'+ch+'p','<input type="checkbox" name = "chListCheck'+ch+'" id="chListCheck'+ch+'"></input>', 'chList'),800,(chControlTop + 15 + 50 * ch),'absolute');
	var imgNode = document.createElement('DIV');
	imgNode.id = "ch"+id+"divImg";
	imgNode.innerHTML = '<img id="ch' + id + 'Img" src="http://cnviewer.googlecode.com/hg/Images/ch' + id + '.png"\>';

	/* div containing ch image and markers */
	var containner = document.createElement('DIV');
	document.getElementById('chList').appendChild(containner);
	containner.appendChild(imgNode);
	containner.id = "ch"+id+"div";
	containner.style.display="none";

	dojo.attr(imgNode,"style",{left: markW + "px", top:"2px", position:"relative", overflow:"hidden"});	

	chromossomes.regions=function(){
	/* correspondence between markers and genomic position*/
		var cnv = document.getElementById("CNV");

		var w = document.getElementById(this.id.replace("obj","Img")).width;
		var rate = cnv.realSize[this.ch]/w;

		for (var i=0; i< this.positions.length; i=i+1){
			cnv.chRegions[this.ch][i]=Math.round(this.positions[i]*rate);
		}

		if (!i) {
			cnv.chRegions[this.ch] = [];
		}
		else {
			var tempArr = copyArray(cnv.chRegions[this.ch])
			cnv.chRegions[this.ch] = quickSort(tempArr);
		}

		return false;
	}

	var node1 = document.createElement("div");
	containner.appendChild(node1);
	node1.id='chSurf'+ch+'node';
	dojo.attr(node1,"style",{left:"0px", top:"0px", position:"absolute"});

	var img = document.getElementById("ch"+id+"Img");

	var height = 23;
	var width = cnv.realSize[ch]*cnv.realSize.rate;
	img.width = width;
	img.height = height;
	dojo.attr(containner, "style", {
		width: width + markW * 2 + "px",
		height: height + 4 + "px",
		left: posit + "px",
		top: (210 + 50 * ch) + "px",
		position: "absolute",
		overflow: "hidden"
	});

	chromossomes.limitMin = cnv.min[ch] * cnv.realSize.rate;
	chromossomes.limitMax = cnv.max[ch] * cnv.realSize.rate;

	var surface = dojox.gfx.createSurface(node1, width + (markW * 2) + "px", height + 4 + "px");

	surface.getEventSource().setAttribute('id', 'chSurf' + ch);

	if ((cnv.chRegions[ch].length==0)||(cnv.chRegions[ch].length==undefined)){

		var shape1 = surface.createPath("m" + chromossomes.limitMin + " 0 " + 
				markW * 2 + " 0 " + markW * (-2) + " " + (height + 4) + " " + 
				markW * 2 + " 0 " + markW * (-2) + " " + 
				((img.height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
					color: "black",
					width: 1.5 + "px",
					cap: "butt",
					join: 4
				}).setTransform({
					dx: 0,
					dy: 0
				});

		shape1.getEventSource().setAttribute('id', 'ch' + id + 'mark01');

		new dojox.gfx.MoveableLimited(shape1);

		var shape2 = surface.createPath("m" + chromossomes.limitMax + " 0 " + 
				markW * 2 + " 0 " + markW * (-2) + " " + (height + 4) + " " + 
				markW * 2 + " 0 " + markW * (-2) + " " + 
				((height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
					color: "black",
					width: 1.5 + "px",
					cap: "butt",
					join: 4
				}).setTransform({
					dx: 0,
					dy: 0
				});

		shape2.getEventSource().setAttribute('id', 'ch' + id + 'mark02');
		new dojox.gfx.MoveableLimited(shape2);


		cnv.chRegions[ch] = [Math.round(chromossomes.limitMin /cnv.realSize.rate ), Math.round(chromossomes.limitMax /cnv.realSize.rate )];
		chromossomes.positions = [Math.round(chromossomes.limitMin), Math.round(chromossomes.limitMax)];
	}
	else{
		for (var i =0; i< cnv.chRegions[ch].length; i=i+2){

			var markA = cnv.chRegions[ch][i] * cnv.realSize.rate;
			var markB = cnv.chRegions[ch][i+1] * cnv.realSize.rate;

			var shape1 = surface.createPath("m" + markA + " 0 " + 
					markW * 2 + " 0 " + markW * (-2) + " " + (height + 4) + " " + 
					markW * 2 + " 0 " + markW * (-2) + " " + 
					((img.height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
						color: "black",
						width: 1.5 + "px",
						cap: "butt",
						join: 4
					}).setTransform({
						dx: 0,
						dy: 0
					});

			shape1.getEventSource().setAttribute('id', 'ch' + id + 'mark01');

			new dojox.gfx.MoveableLimited(shape1);

			var shape2 = surface.createPath("m" + markB + " 0 " + 
					markW * 2 + " 0 " + markW * (-2) + " " + (height + 4) + " " + 
					markW * 2 + " 0 " + markW * (-2) + " " + 
					((height + 4) * -1)).setFill("rgba(16,128,160,0.4)").setStroke({
						color: "black",
						width: 1.5 + "px",
						cap: "butt",
						join: 4
					}).setTransform({
						dx: 0,
						dy: 0
					});

			shape2.getEventSource().setAttribute('id', 'ch' + id + 'mark02');
			new dojox.gfx.MoveableLimited(shape2);
		}			

		for (var i =0; i< cnv.chRegions[ch].length; i=i+1){
			chromossomes.positions[i] = Math.round(cnv.realSize.rate*cnv.chRegions[ch][i]);
		}
	}

	return false;
}

function verifyChSelection(ch){

	var fragment = document.getElementById('CNV').chRegions[ch];
	var probeFinder = new findInArraySorted(document.getElementById('CNV').xAxis[ch]);
	var probeStart = probeFinder.equalOrBiggerValue(fragment[0]);
	var probeEnd = probeFinder.equalOrSmallerValue(fragment[1]);

	if (probeEnd-probeStart<=1) {
		alert("You didn't chose an valid chromosme's area.\n There isn't probes in this area.");

		document.getElementById('CNV').chRegions[ch]=null;
	}

	return false;

}
